home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’87 / Source ƒ.sit / Source ƒ / C ƒ / fastalert.c
Text File  |  1987-03-24  |  5KB  |  195 lines

  1.  
  2. /*         fastalert.c  version 1.0                            March 20, 1987.
  3.  
  4.  
  5.    This file contains four routines in Lightspeed C v2.01 to provide a
  6.    more effecient way to display alert boxes.
  7.    
  8.    The four routines are named FastAlert, FastNoteAlert, FastStopAlert and
  9.    FastCautionAlert. They are functionally equivelant to and have the same
  10.    interface as the system traps Alert, NoteAlert, StopAlert and CautionAlert
  11.    respectively.  When the fast alert routines are called, the image of the
  12.    screen under the alert box is saved.  When they return, the screen image
  13.    is instantly restored and no update event is generated.
  14.    
  15.    Since I wrote them mainly for my own use, I choosed a simple but not 
  16.    the best approach.
  17.  
  18.    The basic algrithem (FastShowAlert) is as follows:
  19.    
  20.    1. Calls GetResource to get the alert template from resource file.
  21.    2. Calculates the boundary rectangle of the alert box from its resource
  22.          template.
  23.    3. Copies the screen image under the alert box to a temporary bit map.
  24.    4. Calls one of the normal alert routine.
  25.    5. Restores the screen image.
  26.    6. Walks down the WindowList and empties the updateRgn field of all
  27.       window records on the list.
  28.    7. Returns the value returned by the normal alert routine.
  29.  
  30.    This method has a major shortcomming.  Since it calls SetEmptyRgn to 
  31.    empty the update region of all windows on the WindowList, all pending
  32.    update events are also lost.  In other words, if your program generates an
  33.    update event and it calls one of the fast alert routines before it gets
  34.    to the main event loop, that update event will not be serviced.
  35.    
  36.    Nevertheless, these routines have been very useful for me and I'd like to
  37.    share them with you.  Please send me suggestions, comments and bug reports
  38.    to the address listed below or to GEnie mail "S.WANG".
  39.    
  40.    Written by:    Sidney Wang
  41.                    Department of Computer Science
  42.                    University of Montana
  43.                    Missoula, MT 59812
  44.                                       
  45.    Note: Some of the bitmap manipulation statements are borrowed from
  46.    Mike Schuster, "Try Pop-Up Menus", The Best of MacTutor, PP 218-225.
  47.  
  48. */
  49.    
  50.  
  51.  
  52. #include    <QuickDraw.h>
  53. #include    <DialogMgr.h>
  54. #include    <MemoryMgr.h>
  55. #include    <stdio.h>
  56.  
  57.  
  58. extern WindowPeek    WindowList : 0x9D6;            /* Low memory global */
  59.  
  60.  
  61. FastAlert( alertID, filterProc )
  62. /* This routine is functionally equivelant to Alert. */
  63.  
  64.     int        alertID;
  65.     ProcPtr    filterProc;
  66.  
  67. {
  68.     return ( FastShowAlert( 0, alertID, filterProc ) );
  69. }
  70.  
  71. /* ---------------------------------------- */
  72.  
  73. FastNoteAlert( alertID, filterProc )
  74. /* This routine is functionally equivelant to NoteAlert. */
  75.  
  76.     int        alertID;
  77.     ProcPtr    filterProc;
  78.  
  79. {
  80.     return ( FastShowAlert( 1, alertID, filterProc ) );
  81. }
  82.  
  83. /* ---------------------------------------- */
  84.  
  85. FastStopAlert( alertID, filterProc )
  86. /* This routine is functionally equivelant to StopAlert. */
  87.  
  88.     int        alertID;
  89.     ProcPtr    filterProc;
  90.  
  91. {
  92.     return ( FastShowAlert( 2, alertID, filterProc ) );
  93. }
  94.  
  95. /* ---------------------------------------- */
  96.  
  97. FastCautionAlert( alertID, filterProc )
  98. /* This routine is functionally equivelant to CautionAlert. */
  99.  
  100.     int        alertID;
  101.     ProcPtr    filterProc;
  102.  
  103. {
  104.     return ( FastShowAlert( 3, alertID, filterProc ) );
  105. }
  106.  
  107. /* ---------------------------------------- */
  108.  
  109. FastShowAlert( alertType, alertID, filterProc )
  110.  
  111.     int        alertType;
  112.     int        alertID;
  113.     ProcPtr    filterProc;
  114.     
  115. {
  116.     typedef    AlertTemplate    **AlertHandle;
  117.     
  118.     AlertHandle        theAlert;
  119.     BitMap            **theAlertBits;
  120.     BitMap            *alertBits;
  121.     int                rowBytes;
  122.     int                rows;
  123.     Rect            alertRect;
  124.     GrafPtr            wMngPort;
  125.     int                resultCode;
  126.     WindowPeek        wTempPtr;
  127.  
  128.     /* Get the alert template from resource file */
  129.     if ( (theAlert = (AlertHandle) GetResource( 'ALRT', alertID )) == NULL )
  130.     {
  131.         return;            /* Alert template not in resource file */
  132.     }
  133.     
  134.     alertRect = (*theAlert)->boundsRect;
  135.     
  136.     /* Enlarge the rectangle to cover the border of an alert box */
  137.     InsetRect( &alertRect, -8, -8 );
  138.     
  139.     rowBytes = ((alertRect.right - alertRect.left + 15) >> 4) << 1;
  140.     rows = alertRect.bottom - alertRect.top;
  141.     
  142.     /* Allocate spece for temporary bit map */
  143.     theAlertBits = (BitMap **) 
  144.             NewHandle( rowBytes * rows + (long) sizeof(BitMap) );
  145.             
  146.     if ( !theAlertBits )
  147.     {
  148.         return;                        /* Out of memory, just return */
  149.     }
  150.     
  151.     HLock( theAlertBits );            /* Now it's save to dereference handle */
  152.     alertBits = *theAlertBits;
  153.     
  154.     /* Construct bit map */
  155.     alertBits->baseAddr = (char *) (alertBits + 1);
  156.     alertBits->rowBytes = rowBytes;
  157.     alertBits->bounds = alertRect;
  158.  
  159.     GetWMgrPort( &wMngPort );
  160.  
  161.     /* Save screen bits under the alert box */
  162.     CopyBits( &wMngPort->portBits, alertBits, &alertBits->bounds,
  163.                 &alertBits->bounds, 0, 0L );
  164.  
  165.     switch ( alertType )
  166.     {
  167.         case 0 : resultCode = Alert( alertID, filterProc ); break;
  168.         
  169.         case 1 : resultCode = NoteAlert( alertID, filterProc ); break;
  170.         
  171.         case 2 : resultCode = StopAlert( alertID, filterProc ); break;
  172.         
  173.         case 3 : resultCode = CautionAlert( alertID, filterProc ); break;
  174.     }
  175.     
  176.     /* Restore screen bits */
  177.     CopyBits( alertBits, &wMngPort->portBits, &alertBits->bounds,
  178.                 &alertBits->bounds, 0, 0L );
  179.  
  180.     HUnlock( theAlertBits );
  181.  
  182.     DisposHandle( theAlertBits );
  183.  
  184.     /* Empty the update regions of all windows */
  185.     wTempPtr = WindowList;
  186.     while ( wTempPtr != NULL )
  187.     {
  188.         SetEmptyRgn( wTempPtr->updateRgn );
  189.         wTempPtr = wTempPtr->nextWindow;
  190.     }
  191.  
  192.     return( resultCode );
  193.     
  194. } /* FastShowAlert */
  195.